package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.RosterExchange;

public class RosterExchangeManager
{
  private List<RosterExchangeListener> rosterExchangeListeners = new ArrayList();
  private Connection con;
  private PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster");
  private PacketListener packetListener;

  public RosterExchangeManager(Connection paramConnection)
  {
    this.con = paramConnection;
    init();
  }

  public void addRosterListener(RosterExchangeListener paramRosterExchangeListener)
  {
    synchronized (this.rosterExchangeListeners)
    {
      if (!this.rosterExchangeListeners.contains(paramRosterExchangeListener))
        this.rosterExchangeListeners.add(paramRosterExchangeListener);
    }
  }

  public void removeRosterListener(RosterExchangeListener paramRosterExchangeListener)
  {
    synchronized (this.rosterExchangeListeners)
    {
      this.rosterExchangeListeners.remove(paramRosterExchangeListener);
    }
  }

  public void send(Roster paramRoster, String paramString)
  {
    Message localMessage = new Message(paramString);
    RosterExchange localRosterExchange = new RosterExchange(paramRoster);
    localMessage.addExtension(localRosterExchange);
    this.con.sendPacket(localMessage);
  }

  public void send(RosterEntry paramRosterEntry, String paramString)
  {
    Message localMessage = new Message(paramString);
    RosterExchange localRosterExchange = new RosterExchange();
    localRosterExchange.addRosterEntry(paramRosterEntry);
    localMessage.addExtension(localRosterExchange);
    this.con.sendPacket(localMessage);
  }

  public void send(RosterGroup paramRosterGroup, String paramString)
  {
    Message localMessage = new Message(paramString);
    RosterExchange localRosterExchange = new RosterExchange();
    Iterator localIterator = paramRosterGroup.getEntries().iterator();
    while (localIterator.hasNext())
    {
      RosterEntry localRosterEntry = (RosterEntry)localIterator.next();
      localRosterExchange.addRosterEntry(localRosterEntry);
    }
    localMessage.addExtension(localRosterExchange);
    this.con.sendPacket(localMessage);
  }

  private void fireRosterExchangeListeners(String paramString, Iterator<RemoteRosterEntry> paramIterator)
  {
    RosterExchangeListener[] arrayOfRosterExchangeListener = null;
    synchronized (this.rosterExchangeListeners)
    {
      arrayOfRosterExchangeListener = new RosterExchangeListener[this.rosterExchangeListeners.size()];
      this.rosterExchangeListeners.toArray(arrayOfRosterExchangeListener);
    }
    for (int i = 0; i < arrayOfRosterExchangeListener.length; i++)
      arrayOfRosterExchangeListener[i].entriesReceived(paramString, paramIterator);
  }

  private void init()
  {
    this.packetListener = new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Message localMessage = (Message)paramAnonymousPacket;
        RosterExchange localRosterExchange = (RosterExchange)localMessage.getExtension("x", "jabber:x:roster");
        RosterExchangeManager.this.fireRosterExchangeListeners(localMessage.getFrom(), localRosterExchange.getRosterEntries());
      }
    };
    this.con.addPacketListener(this.packetListener, this.packetFilter);
  }

  public void destroy()
  {
    if (this.con != null)
      this.con.removePacketListener(this.packetListener);
  }

  protected void finalize()
    throws Throwable
  {
    destroy();
    super.finalize();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.RosterExchangeManager
 * JD-Core Version:    0.6.1
 */